package com.planw.beetl.sql;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.ElementPatternCondition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.JavaAnnotationElementType;
import com.intellij.psi.impl.source.tree.FileElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.ui.JBColor;
import com.intellij.util.PlatformIcons;
import com.intellij.util.ProcessingContext;
//import org.intellij.sdk.language.p.SimpleTypes;
import com.planw.beetl.utils.JavaUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SimpleCompletionContributor extends CompletionContributor {

    public SimpleCompletionContributor() {
        extend(CompletionType.BASIC, new MyElementPattern(),
                new MyCompletionProvider()
        );
    }
    static class MyElementPattern implements ElementPattern{

        @Override
        public boolean accepts(@Nullable Object o) {
            return true;
        }
        @Override
        public boolean accepts(@Nullable Object o, ProcessingContext context) {
            return true;
        }

        @Override
        public ElementPatternCondition getCondition() {
            return null;
        }
    }

    static class MyCompletionProvider extends CompletionProvider<CompletionParameters>{



        @Override
        protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
            PsiFile psiFile = parameters.getOriginalFile();
            FileType fileType=  parameters.getOriginalFile().getFileType();
            Language language =  parameters.getOriginalFile().getLanguage();
            if(!language.getID().equals("Markdown")){
                return ;
            }

            int offset = parameters.getOffset();
            PsiElement current = parameters.getPosition();

            PsiElement parent = null;
            while((parent= current.getParent())!=null){
                String text = parent.getText();
                ASTNode node = parent.getNode();
                if(node.getTreeParent() instanceof FileElement){
                    int methodIndex =  text.indexOf("===");
                    String method = text.substring(0,methodIndex).trim();
                    String fileName = psiFile.getName();
                    tips(current,fileName,method,result);

                    break;

                }
                IElementType elementType = node.getElementType();
                current = parent;
            }
        }
        void tips(PsiElement current,String fileName, String method,CompletionResultSet result){
            List tips = new ArrayList();
            fileName  = fileName.split("\\.")[0];
            fileName = StringUtils.capitalize(fileName);
            Project project = current.getProject();
            List<PsiClass>  list =  JavaUtils.findMapperClass(fileName,project);
            List<PsiMethod> methods = JavaUtils.loadMapperPsiMethod(list,method);
            methods.forEach(psiMethod -> {
                PsiParameterList parameterList = psiMethod.getParameterList();
                PsiParameter[] psiParameters =  parameterList.getParameters();
                for(int i=0;i<psiParameters.length;i++){
                    PsiParameter psiParameter =  psiParameters[i];
                    String name = psiParameter.getName();
                    String typeName =  psiParameter.getType().getPresentableText();
                    LookupElementBuilder lookupElementBuilder =  LookupElementBuilder.create(name)
                            .withItemTextForeground(JBColor.RED)
                            .bold()
                            .withIcon(PlatformIcons.VARIABLE_ICON)
                            .withTypeText(typeName, PlatformIcons.VARIABLE_ICON, true)
                            .withTypeIconRightAligned(true);
                    result.addElement(lookupElementBuilder);
                }
            });

            addOthers(result);
        }

        protected  void addOthers(CompletionResultSet result){
            LookupElementBuilder isEmpty =  LookupElementBuilder.create("isEmpty")
                    .withItemTextForeground(JBColor.GRAY)
                    .bold()
                    .withIcon(PlatformIcons.VARIABLE_ICON)
                    .withTypeText("function", PlatformIcons.FUNCTION_ICON, true)
                    .withTypeIconRightAligned(true);;
            LookupElementBuilder isNotEmpty =  LookupElementBuilder.create("isNotEmpty")
                    .withItemTextForeground(JBColor.GRAY)
                    .bold()
                    .withIcon(PlatformIcons.VARIABLE_ICON)
                    .withTypeText("function", PlatformIcons.FUNCTION_ICON, true)
                    .withTypeIconRightAligned(true);;
            result.addElement(isEmpty);
            result.addElement(isNotEmpty);
        }
    }



}